package com.headwind.logistics.system.controller;

import com.headwind.logistics.common.pojo.Customer;
import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.util.LayuiUtil;
import com.headwind.logistics.log.annotate.Log;
import com.headwind.logistics.system.dto.WorkOrderEmployeeUseDto;
import com.headwind.logistics.system.pojo.WorkOrder;
import com.headwind.logistics.system.service.intl.WorkOrderServiceIntl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 桂祥宇
 * @Date 2020/7/26 0026 15:09
 * @Version 1.0
 * @DESCRIPTION 工单控制器层
 */
@Controller
public class WorkOrderController {

    //工单业务逻辑层接口
    @Resource
    private WorkOrderServiceIntl workOrderServiceIntl;


    @GetMapping("/admin/system/queryAllWorkOrderEmployeeUseDto")
    @ResponseBody
    @Log("查询所有工单")
    public LayuiUtil queryAllWorkOrderEmployeeUseDto(Integer page, Integer limit){
       List<WorkOrderEmployeeUseDto> workOrderEmployeeUseDtos=workOrderServiceIntl.queryAllWorkOrderEmployeeUseDto(page,limit);

       return LayuiUtil.data(workOrderServiceIntl.queryAllWorkOrderEmployeeUseDto(null,null).size(),workOrderEmployeeUseDtos);
    }

    @GetMapping("/admin/system/deleteWorkOrder")
    @ResponseBody
    @Log("根据id删除工单")
    public int deleteWorkOrder(int workOrderId){

        int count =workOrderServiceIntl.deleteWorkOrder(workOrderId);
        return count;
    }

    @GetMapping("/admin/system/queryAllEmployees")
    @ResponseBody
    @Log("查询所有员工号和员工名字")
    public List<Employee> queryAllEmployees(){

        List<Employee> employee =workOrderServiceIntl.queryAllEmployees();
        for (Employee employee1:employee){
            System.out.println(employee1.getEmployeeName());
        }
        return employee;
    }


    @GetMapping("/admin/system/addWorkOrders")
    @ResponseBody
    @Log("新增工单")
    public int addWorkOrder(WorkOrder workOrder){

        int are= Integer.parseInt(workOrder.getCutomerName());
        Customer customer=workOrderServiceIntl.queryAllCustomerid(are);
        WorkOrder workOrder1=new WorkOrder();
        workOrder1.setWaybillNo(workOrder.getWaybillNo());
        workOrder1.setQuestionType(workOrder.getQuestionType());
        workOrder1.setWorkOrderStatus(workOrder.getWorkOrderStatus());
        workOrder1.setWorkOrderBeginTime(workOrder.getWorkOrderBeginTime());
        workOrder1.setEmployeeNo(workOrder.getEmployeeNo());
        workOrder1.setStarLevel(workOrder.getStarLevel());
        workOrder1.setCutomerName(customer.getUserName());
        workOrder1.setCustomerTel(customer.getPhone());
        int count =workOrderServiceIntl.addWorkOrder(workOrder1);
        return count;
    }

    @GetMapping("/admin/system/updateWorkOrder")
    @ResponseBody
    @Log("修改工单")
    public int updateWorkOrder(WorkOrder workOrder){
        int are= Integer.parseInt(workOrder.getCutomerName());
        Customer customer=workOrderServiceIntl.queryAllCustomerid(are);
        System.out.println(customer.getUserName());
        System.out.println(customer.getPhone());
        WorkOrder workOrders=new WorkOrder();
        workOrders.setWorkOrderId(workOrder.getWorkOrderId());
        workOrders.setWaybillNo(workOrder.getWaybillNo());
        workOrders.setQuestionType(workOrder.getQuestionType());
        workOrders.setWorkOrderStatus(workOrder.getWorkOrderStatus());
        workOrders.setWorkOrderBeginTime(workOrder.getWorkOrderBeginTime());
        workOrders.setEmployeeNo(workOrder.getEmployeeNo());
        workOrders.setStarLevel(workOrder.getStarLevel());
        workOrders.setCutomerName(customer.getUserName());
        workOrders.setCustomerTel(customer.getPhone());
        int count =workOrderServiceIntl.updateWorkOrder(workOrders);
        return count;
    }
}
